package com.example.shop_2210.detail

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2210.R
import com.example.shop_2210.databinding.ActivityDetailBinding

/**
 * 编译报错----var
 * 进入详情页，报错---string IX0    价格/库存/销量没有转成字符串
 * 进入详情，不显示数据----goodsId 两边不一致，导致goodsId没有赋值成功
 */
@Route(path = "/app/DetailActivity")
class DetailActivity : BaseActivity<ActivityDetailBinding,DetailViewModel>() {
    @Autowired
    @JvmField
    var goodsId = 1

    var attrId = 0;//加入购物车使用 属性Id
    override fun initData() {
        viewModel.detailSuccess.observe(this){
            if(it.code==200){
                binding.info = it.data.productInfo
                attrId = it.data.productAttr[0].id
            }
        }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){

            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.detail(goodsId,"normal")
        //加入购物车
        binding.addCar.setOnClickListener {
            viewModel.add(1,attrId.toString(),goodsId)
        }
    }

    override fun getViewModel(): Class<DetailViewModel>  = DetailViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_detail

}